<script setup>
import { reactive, ref, onMounted } from 'vue';
import { useRouter } from 'vue-router'
import userApi from '../api/User_api.js'
import loginApi from '../api/Login_api.js'
import {ElMessage} from "element-plus";

const user = ref({
  userNumber: '',
  userPassword: '',
  confirmPassword: '',
  userNickname: '',
});

const router = useRouter();

// 表单验证规则
const rules = reactive({
  userNumber: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
  ],
  userPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== user.value.userPassword) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ],
  userNickname: [
    { required: true, message: '请输入昵称', trigger: 'blur' },
    { min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur' }
  ]
});

const onSubmit = async () => {
  try {
    // 调用注册API
    await userApi.register(user.value);
    ElMessage.success('注册成功，请登录');
    await router.push('/login');
  } catch (error) {
    console.error('注册失败', error);
    ElMessage.error('注册失败: ' + (error.response?.data?.msg || error.message));
  }
}

const toLogin = () => {
  router.push('/login');
}
</script>

<template>
  <div class="register-bg">
    <el-form class="form-register" :model="user" :rules="rules" label-width="80px">
      <h1>用户注册</h1>

      <el-form-item label="账号" prop="userNumber">
        <el-input v-model="user.userNumber" placeholder="请输入账号" maxlength="16"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="userPassword">
        <el-input v-model="user.userPassword" placeholder="请输入密码" show-password maxlength="16"></el-input>
      </el-form-item>

      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="user.confirmPassword" placeholder="请再次输入密码" show-password maxlength="16"></el-input>
      </el-form-item>

      <el-form-item label="昵称" prop="userNickname">
        <el-input v-model="user.userNickname" placeholder="请输入昵称" maxlength="16"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">注册</el-button>
        <el-button @click="toLogin">返回登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>
.register-bg {
  height: 100vh;
  background-image: url('../assets/back.png');
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

.form-register {
  width: 400px;
  padding: 30px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

h1 {
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
}
</style>